package com.centling.smartSealForPhone.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;
import com.centling.smartSealForPhone.constants.UrlConstants;
import com.fionera.base.util.MD5Util;
import com.centling.smartSealForPhone.utils.OkHttpUtil;
import com.centling.smartSealForPhone.utils.ShowToast;

import java.util.HashMap;
import java.util.Map;

public class ResetUserPasswordActivity extends TitleBarActivity {
    private EditText pass;
    private EditText passR;
    private TextView sure;
    private TextView cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_user_password);
        setTitleBarText("重置密码");
        initView();
    }

    public void initView() {
        pass = (EditText) findViewById(R.id.reset_user_pass);
        passR = (EditText) findViewById(R.id.reset_user_passR);
        sure = (TextView) findViewById(R.id.reset_user_sure);
        cancel = (TextView) findViewById(R.id.reset_user_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(pass.getText().toString())) {
                    ShowToast.shortTime("请输入密码");
                    return;
                }
                if (TextUtils.isEmpty(pass.getText().toString())) {
                    ShowToast.shortTime("请输入密码");
                    return;
                }
                if (pass.getText().toString().length() < 6) {
                    ShowToast.shortTime("请输入6~20位密码");
                    return;
                }
                if (!pass.getText().toString().equals(passR.getText().toString())) {
                    ShowToast.shortTime("两次密码输入不一致");
                    return;
                }

                String newPass = pass.getText().toString();
                mProcessDialog.setTitle("正在重置密码").showNormal();
                Map<String, String> param = new HashMap<>();
                param.put("nnewPassword", MD5Util.md5(newPass));
                String userId = getIntent().getStringExtra("user_id");
                putJsonEnqueue(UrlConstants.RESET_USER_PASS + userId, param,
                        new OkHttpUtil.NetCallBack() {
                            @Override
                            public void onSucceed(String json) {
                                ShowToast.shortTime("重置密码成功");
                                mProcessDialog.dismiss();
                                finish();
                            }

                            @Override
                            public void onFailed(String reason) {
                                mProcessDialog.dismiss();
                                ShowToast.shortTime(reason);
                            }
                        });
            }
        });
    }
}
